import * as Blockly from 'blockly/core';

const COMMUNICATE_HUE = 140;

// 红外接收模块
export const ir_recv = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendValueInput("PIN", Number)
            .appendField(new Blockly.FieldTextInput('ir_item'), 'VAR')
            .appendField(Blockly.Msg.MIXLY_IR_RECEIVE)
            .setCheck(Number);
        this.appendStatementInput('DO')
            .appendField(Blockly.Msg.MIXLY_IR_RECEIVE_YES);
        this.appendStatementInput('DO2')
            .appendField(Blockly.Msg.MIXLY_IR_RECEIVE_NO);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_IR_RECIEVE_TOOLTIP);
    },
    getVars: function () {
        return [this.getFieldValue('VAR')];
    },
    renameVar: function (oldName, newName) {
        if (Blockly.Names.equals(oldName, this.getFieldValue('VAR'))) {
            this.setTitleValue(newName, 'VAR');
        }
    }
};

const NEC_TYPE = [
    ['RC5', 'RC5'],
    ['RC6', 'RC6'],
    ['NEC', 'NEC'],
    ['Sony', 'Sony'],
    ['Panasonic', 'Panasonic'],
    ['JVC', 'JVC'],
    ['SAMSUNG', 'SAMSUNG'],
    ['Whynter', 'Whynter'],
    ['AiwaRCT501', 'AiwaRCT501'],
    ['LG', 'LG'],
    ['Sanyo', 'Sanyo'],
    ['Mitsubishi', 'Mitsubishi'],
    ['DISH', 'DISH'],
    ['SharpRaw', 'SharpRaw'],
    ['Denon', 'Denon']
];

// 红外发射模块（NEC）
export const ir_send_nec = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendDummyInput("")
            .appendField(Blockly.Msg.MIXLY_IR_SEND_NEC1)
            .appendField(new Blockly.FieldDropdown(NEC_TYPE), 'TYPE')
            .appendField(Blockly.Msg.MIXLY_IR_SEND_NEC2)
            .appendField(new Blockly.FieldDropdown([["3", "3"]]), "PIN");
        this.appendValueInput('data')
            .setCheck(Number)
            .appendField(' ' + Blockly.Msg.MIXLY_DATA);
        this.appendValueInput('bits')
            .setCheck(Number)
            .appendField(' ' + Blockly.Msg.MIXLY_BITS);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_IR_SEND_NEC_TOOLTIP);
    }
};

// 红外接收使能
export const ir_recv_enable = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendValueInput("PIN", Number)
            .appendField(Blockly.Msg.MIXLY_IR_RECEIVE_ENABLE);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_IR_ENABLE);
    }
};

// 红外接收模块(raw)
export const ir_recv_raw = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendValueInput("PIN", Number)
            .appendField(Blockly.Msg.MIXLY_IR_RECEIVE_RAW)
            .setCheck(Number);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_IR_RECIEVE_RAW_TOOLTIP);
    }
};

// 红外发射模块(raw)
export const ir_send_raw = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendDummyInput("")
            .appendField(Blockly.Msg.MIXLY_IR_SEND_RAW)
            .appendField(new Blockly.FieldDropdown([["3", "3"]]), "PIN");
        this.appendDummyInput("")
            .appendField(' ' + Blockly.Msg.MIXLY_I2C_SLAVE_WRITE_ARRAY_ARRAYNAME)
            .appendField(new Blockly.FieldTextInput('0,0,0'), 'TEXT');
        this.appendValueInput('length')
            .setCheck(Number)
            .appendField(' ' + Blockly.Msg.MIXLY_LIST_LENGTH);
        this.appendValueInput('freq')
            .setCheck(Number)
            .appendField(' ' + Blockly.Msg.MIXLY_FREQUENCY);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip(Blockly.Msg.MIXLY_IR_SEND_RAW_TOOLTIP);
    }
};

// IIC通信
// IIC初始化主机
export const i2c_master_Init = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.MIXLY_SETUP + ' I2C ' + Blockly.Msg.MIXLY_MASTER);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(' ' + Blockly.Msg.CONTROLS_FOR_INPUT_WITH)
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C1(" + Blockly.Msg.MIXLY_STM32_SPI_REMAP + ")", "remap_I2C_1"], ["I2C2", "Wire1"]]), "i2c_type");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(COMMUNICATE_HUE);
        this.setTooltip();
        this.setHelpUrl("");
    }
};

// IIC初始化从机
export const i2c_slave_Init = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.MIXLY_SETUP + ' I2C ' + Blockly.Msg.MIXLY_SALVE);
        this.appendValueInput("i2c_address")
            .setCheck(null)
            .appendField(' ' + Blockly.Msg.MIXLY_LCD_ADDRESS);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(' ' + Blockly.Msg.CONTROLS_FOR_INPUT_WITH)
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C1(" + Blockly.Msg.MIXLY_STM32_SPI_REMAP + ")", "remap_I2C_1"], ["I2C2", "Wire1"]]), "i2c_type");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(COMMUNICATE_HUE);
        this.setTooltip(Blockly.Msg.MIXLY_I2C_MASTER_INITHelp);
        this.setHelpUrl("");
    }
};

// IIC主机发送数据
export const i2c_begin_end_transmission = {
    init: function () {
        this.appendDummyInput()
            .appendField("I2C " + Blockly.Msg.MIXLY_MASTER)
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(' ' + Blockly.Msg.MIXLY_SEND_DATA);
        this.appendValueInput("i2c_address")
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Blockly.Msg.MIXLY_LCD_ADDRESS);
        this.appendStatementInput("transmission_data")
            .setCheck(null);
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(COMMUNICATE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

// IIC写入主从机数据
export const i2c_write = {
    init: function () {
        this.appendValueInput("i2c_write_data")
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("I2C")
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(" " + Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_SEND);
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(COMMUNICATE_HUE);
        this.setTooltip("");
    }
};

// IIC写入数线数据
export const i2c_slave_write_array = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendValueInput('array')
            .appendField("I2C")
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(" " + Blockly.Msg.MIXLY_STM32_I2C_SLAVE_SEND_ARRAY)
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Blockly.Msg.MIXLY_I2C_SLAVE_WRITE_ARRAY_ARRAYNAME);
        this.appendValueInput('length')
            .setCheck(Number)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Blockly.Msg.MIXLY_LIST_LEN);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_I2C_SLAVE_WRITE_ARRAY);
    }
};

// IIC主机或从机读取成功吗？
export const i2c_available = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendDummyInput()
            .appendField("I2C")
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(" " + Blockly.Msg.MIXLY_AVAILABLE);
        this.setOutput(true, Boolean);
        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_I2C_AVAILABLE);
    }
};

// IIC主机或从机读取的数据
export const i2c_read = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendDummyInput()
            .appendField("I2C")
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(" " + Blockly.Msg.MIXLY_ESP32_READ);
        this.setOutput(true, Number);
        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_I2C_MASTER_READ2);
    }
};

// 写入寄存器地址
export const i2c_master_writerReg = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendValueInput('device')
            .setCheck(Number)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("I2C")
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(" " + Blockly.Msg.MIXLY_WRITE)
            .appendField(Blockly.Msg.MIXLY_LCD_ADDRESS);
        this.appendValueInput('regadd')
            .setCheck(Number)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Blockly.Msg.MIXLY_I2C_REGADD);
        this.appendValueInput('value')
            .setCheck(Number)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Blockly.Msg.MIXLY_MICROBIT_JS_I2C_VALUE);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
    }
};

// 读取寄存器地址
export const i2c_master_readerReg = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendValueInput('device')
            .setCheck(Number)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("I2C")
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(" " + Blockly.Msg.MIXLY_SERIAL_READ)
            .appendField(Blockly.Msg.MIXLY_LCD_ADDRESS);
        this.appendValueInput('regadd')
            .setCheck(Number)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Blockly.Msg.MIXLY_I2C_REGADD);
        this.appendValueInput('bytes')
            .setCheck(Number)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Blockly.Msg.MIXLY_I2C_BYTES);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setInputsInline(true);
    }
};

export const i2c_slave_onrequest = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendDummyInput("")
            .appendField("I2C")
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(" " + Blockly.Msg.MIXLY_STM32_I2C_SLAVE_SEND_EVENT);
        this.appendStatementInput('DO')
            .appendField(Blockly.Msg.MIXLY_DO);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_I2C_SLAVE_ONREQUEST);
    }
};

export const i2c_master_writer = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendValueInput('device')
            .setCheck(Number)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("I2C")
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(" " + Blockly.Msg.MIXLY_ESP32_WRITE)
            .appendField(Blockly.Msg.MIXLY_LCD_ADDRESS);
        this.appendValueInput('value')
            .setCheck([String, Number])
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Blockly.Msg.HTML_VALUE);
        this.setPreviousStatement(true);
        this.setNextStatement(true);

        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_I2C_MASTER_WRITE);
    }
};
export const i2c_master_reader = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendValueInput('device')
            .setCheck(Number)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField("I2C")
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(" " + Blockly.Msg.MIXLY_SERIAL_READ)
            .appendField(Blockly.Msg.MIXLY_LCD_ADDRESS);
        this.appendValueInput('bytes')
            .setCheck(Number)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Blockly.Msg.MIXLY_I2C_BYTES);
        this.setInputsInline(true);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_I2C_MASTER_READ);
    }
};

export const i2c_master_reader2 = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendDummyInput()
            .appendField("I2C")
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(" " + Blockly.Msg.MIXLY_SERIAL_READ);
        this.setOutput(true, Number);
        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_I2C_MASTER_READ2);
    }
};

export const i2c_slave_onreceive = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendDummyInput()
            .appendField("I2C")
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(' ' + Blockly.Msg.MIXLY_STM32_I2C_SLAVE_RECEIVE_EVENT);
        this.appendValueInput("onReceive_length")
            .setCheck(null)
            .appendField(Blockly.Msg.MIXLY_I2C_BYTES);
        this.appendStatementInput('DO')
            .appendField(Blockly.Msg.MIXLY_DO);
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_I2C_SLAVE_ONRECEIVE);
    }
};

export const i2c_slave_write = {
    init: function () {
        this.setColour(COMMUNICATE_HUE);
        this.appendValueInput('value')
            .appendField("I2C")
            .appendField(new Blockly.FieldDropdown([["I2C1", "Wire"], ["I2C2", "Wire1"]]), "i2c_type")
            .appendField(" " + Blockly.Msg.MIXLY_STM32_I2C_SLAVE_SEND_BYTE)
            .setCheck(Number)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Blockly.Msg.HTML_VALUE);
        this.setPreviousStatement(true);
        this.setNextStatement(true);
        this.setTooltip(Blockly.Msg.MIXLY_TOOLTIP_I2C_SLAVE_WRITE);
    }
};

// SPI 初始化从机
export const spi_begin_slave = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.MIXLY_SETUP + " SPI " + Blockly.Msg.MIXLY_SALVE)
            .appendField(' ' + Blockly.Msg.CONTROLS_FOR_INPUT_WITH)
            .appendField(new Blockly.FieldDropdown([["SPI1", "SPI_1"], ["SPI1(" + Blockly.Msg.MIXLY_STM32_SPI_REMAP + ")", "remap_SPI_1"], ["SPI2", "SPI_2"], ["SPI3", "SPI_3"]]), "spi_type");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(COMMUNICATE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

export const spi_begin_master = {
    init: function () {
        this.appendDummyInput()
            .appendField(Blockly.Msg.MIXLY_SETUP + " SPI " + Blockly.Msg.MIXLY_MASTER);
        this.appendValueInput("spi_slave_pin")
            .setCheck(null)
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Blockly.Msg.MIXLY_SALVE + Blockly.Msg.MIXLY_PIN);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(' ' + Blockly.Msg.CONTROLS_FOR_INPUT_WITH)
            .appendField(new Blockly.FieldDropdown([["SPI1", "SPI_1"], ["SPI1(" + Blockly.Msg.MIXLY_STM32_SPI_REMAP + ")", "remap_SPI_1"], ["SPI2", "SPI_2"], ["SPI3", "SPI_3"]]), "spi_type");
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(COMMUNICATE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

export const spi_transfer_Init = {
    init: function () {
        this.appendValueInput("slave_pin")
            .setCheck(null)
            .appendField("SPI")
            .appendField(' ' + Blockly.Msg.MIXLY_SEND_DATA + ' ' + Blockly.Msg.MIXLY_SALVE + Blockly.Msg.MIXLY_PIN);
        this.appendStatementInput("transfer_data")
            .setCheck(null);
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(COMMUNICATE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

export const spi_transfer_1 = {
    init: function () {
        this.appendValueInput("transfer_data")
            .setCheck(null)
            .appendField("SPI")
            .appendField(new Blockly.FieldDropdown([["SPI1", "SPI_1"], ["SPI2", "SPI_2"], ["SPI3", "SPI_3"]]), "spi_type")
            .appendField(" " + Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_SEND);
        this.setInputsInline(true);
        this.setPreviousStatement(true, null);
        this.setNextStatement(true, null);
        this.setColour(COMMUNICATE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};

export const spi_transfer_2 = {
    init: function () {
        this.appendValueInput("transfer_data")
            .setCheck(null)
            .appendField("SPI")
            .appendField(new Blockly.FieldDropdown([["SPI1", "SPI_1"], ["SPI2", "SPI_2"], ["SPI3", "SPI_3"]]), "spi_type")
            .appendField(" " + Blockly.Msg.MIXLY_MICROPYTHON_SOCKET_SEND);
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_RIGHT)
            .appendField(Blockly.Msg.MIXLY_RETURN_DATA);
        this.setInputsInline(true);
        this.setOutput(true, null);
        this.setColour(COMMUNICATE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    }
};